package com.utils;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.model.StageInfo;
import com.test.R;

public class StageInfoAdapter extends ArrayAdapter<StageInfo> {

	private List<StageInfo> stageInfos;
	private Context context;
	private String PATH = "pdfReport/";
	

	public StageInfoAdapter(Context context, int textViewResourceId,
			List<StageInfo> stageInfos) {
		super(context, textViewResourceId, stageInfos);
		this.stageInfos = stageInfos;
		this.context = context;
		
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View view = convertView;
		if (view == null) {
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
			view = vi.inflate(R.layout.itemrenderer, null);
		}

		final StageInfo info = stageInfos.get(position);
		if (info != null) {

			TextView tt = (TextView) view.findViewById(R.id.topText);
			TextView firstbt = (TextView) view.findViewById(R.id.firstBottomText);
			TextView secondbt = (TextView) view.findViewById(R.id.secondBottomText);
			TextView thirdbt = (TextView) view.findViewById(R.id.thirdBottomText);
			TextView forthbt = (TextView) view.findViewById(R.id.forthBottomText);
			ImageView downloadicon=(ImageView) view.findViewById(R.id.icondownload);

			if (tt != null) {
				tt.setText(info.getStudent_fullname());
				tt.setTextSize(22);
				tt.setTypeface(Typeface.DEFAULT_BOLD);
			}

			if (firstbt != null)
			{
				firstbt.setText(info.getProgramme() + info.getPromo());
				firstbt.setTextSize(15);
				firstbt.setTypeface(Typeface.DEFAULT_BOLD);
			}
			
			if (secondbt != null)
			{
				secondbt.setText(info.getMajeure());
				secondbt.setTextSize(15);
				secondbt.setTypeface(Typeface.DEFAULT_BOLD);
				
			}
			
			if (thirdbt != null)
			{
				thirdbt.setText("Plagiat: "+info.getPlagiat_percentage()+"%");
				firstbt.setTextSize(15);
				
			}

			if (forthbt != null) {
				forthbt.setText("Status: " + info.getStatus());

				forthbt.setTypeface(Typeface.DEFAULT_BOLD);

				if (info.getStatus().equalsIgnoreCase("attente")) {
					forthbt.setTextColor(Color.RED);
				} else if (info.getStatus().equalsIgnoreCase("valide")) {
					forthbt.setTextColor(Color.GREEN);
				} else if (info.getStatus().equalsIgnoreCase("archived")) {
					forthbt.setTextColor(Color.GRAY);
				}
			}
			
			if(downloadicon!=null){
				String reportName= info.getFileInfo()+"_RapportOriginal.pdf";
				this.btnSensitive(PATH, reportName, downloadicon);
			}
		}

		return view;
	}

	public void uploadNotification(int flag) {
		if (flag == 1) {
			Toast msg = Toast.makeText(context, R.string.upload_file_not_exist,
					Toast.LENGTH_SHORT);
			msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2,
					msg.getYOffset() / 2);
			msg.show();
		}

		else if (flag == -1) {
			Toast msg = Toast.makeText(context, R.string.upload_error,
					Toast.LENGTH_SHORT);
			msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2,
					msg.getYOffset() / 2);
			msg.show();
		}

		else if (flag == 0) {
			Toast msg = Toast.makeText(context, R.string.upload_success,
					Toast.LENGTH_SHORT);
			msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2,
					msg.getYOffset() / 2);
			msg.show();

		}

	}

	public void downloadNotification(int flag) {
		if (flag == 1) {
			Toast msg = Toast.makeText(context, R.string.download_exist,
					Toast.LENGTH_SHORT);
			msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2,
					msg.getYOffset() / 2);
			msg.show();
		}

		else if (flag == -1) {
			Toast msg = Toast.makeText(context, R.string.download_error,
					Toast.LENGTH_SHORT);
			msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2,
					msg.getYOffset() / 2);
			msg.show();
		}

		else if (flag == 0) {
			Toast msg = Toast.makeText(context, R.string.download_success,
					Toast.LENGTH_SHORT);
			msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2,
					msg.getYOffset() / 2);
			msg.show();

		}
	}
	
	public void btnSensitive(String path, String reportName, ImageView icon)
	{
		FileUtils util=new FileUtils();
		if(util.isFileExist(path +reportName)){
			icon.setImageResource(R.drawable.upload_icon);
		}else{
			icon.setImageResource(R.drawable.download_icon);
		}
		
		
	}



}
